﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using C1.Phone.Chart;
using System.Windows.Data;
using Microsoft.Phone.Shell;
using System.Text;

namespace ControlExplorer.Controls.Chart
{
    public partial class AggregateChart
        : UserControl, IExposeApplicationBarItems
    {
        Dictionary<string, Brush> _dict = new Dictionary<string, Brush>();
        List<ApplicationBarMenuItem> _menuItems = new List<ApplicationBarMenuItem>();

        public AggregateChart()
        {
            InitializeComponent();

            foreach (var value in C1.Phone.Extensions.GetEnumValues<Aggregate>(typeof(Aggregate)))
            {
                var mitem = new ApplicationBarMenuItem()
                {
                    Text = Utils.SeparateWords(value.ToString())
                };
                mitem.Click += mitem_Click;
                _menuItems.Add(mitem);
            }

            Loaded += AggregateChart_Loaded;
        }

        void AggregateChart_Loaded(object sender, RoutedEventArgs e)
        {
            _dict["red"] = CreateBrush(Colors.Red);
            _dict["blue"] = CreateBrush(Color.FromArgb(255, 2, 150, 252));
            _dict["yellow"] = CreateBrush(Colors.Yellow);
            _dict["white"] = CreateBrush(Colors.White);


            int cnt = 10;
            chart.Data.ItemNames = CreateRandomStrings(cnt,
              new string[] { "blue", "red", "white", "yellow" }); ;

            var vals = CreateRandomValues(cnt);

            var ds = new DataSeries() { ValuesSource = vals };

            ds.PlotElementLoaded += (s, e2) =>
            {
                PlotElement pe = (PlotElement)s;
                if (_dict.ContainsKey(pe.DataPoint.Name))
                    pe.Fill = _dict[pe.DataPoint.Name];
                pe.StrokeThickness = 0;
            };

            chart.Data.Children.Add(ds);
            chart.View.AxisX.AnnoTemplate = chart.Resources["al_tmpl"];
            BarColumnOptions.SetRadiusX(chart, 4);
            BarColumnOptions.SetRadiusY(chart, 4);
        }

        void mitem_Click(object sender, EventArgs e)
        {                        
            var menuItem = (ApplicationBarMenuItem) sender;
            chart.Aggregate = (Aggregate) _menuItems.IndexOf(menuItem);
        }

        Random rnd = new Random();

        double[] CreateRandomValues(int cnt)
        {
            double[] vals = new double[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = rnd.NextDouble() * 100;
            return vals;
        }

        string[] CreateRandomStrings(int cnt, string[] list)
        {
            string[] vals = new string[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = list[rnd.Next(0, list.Length)];
            return vals;
        }

        Brush CreateBrush(Color clr)
        {
            return new LinearGradientBrush()
            {
                GradientStops = 
                { 
                  //new GradientStop() { Color= Colors.Black,  Offset = 0},
                  new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 0},
                  new GradientStop() { Color= clr, Offset = 0.2},
                  new GradientStop() { Color= clr, Offset = 0.7},
                  new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 1},
                },
                StartPoint = new Point(0, 0),
                EndPoint = new Point(1, 1),
                //Opacity = 0.8
            };
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                return _menuItems.Cast<IApplicationBarMenuItem>();
            }
        }

        #endregion
    }

    public class AxisLabelConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
                return string.Format("../../../Resources/flower_{0}.png", value.ToString());

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}